package org.example.demo1startjava17.filter;


import jakarta.servlet.*;
import jakarta.servlet.annotation.WebFilter;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * 配置方式2
 * 1.实现Filter接口
 * 2.添加注解@Component
 * // TODO 没找到指定过滤路径的地方，该方法只是filter3的简化版，无指定路径功能 vash
 *
 */
@Slf4j
@Component
@Order(value = 3)
public class MyFilter2 implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
//        log.info("MyFilter 初始化了");
        System.err.println("MyFilter2 初始化了");
        Filter.super.init(filterConfig);
//        System.err.println(filterConfig.getFilterName());
//        System.err.println(JSON.toJSONString(filterConfig.getServletContext()));
//        System.err.println(JSON.toJSONString(filterConfig.getInitParameterNames()));
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;
//        log.info("MyFilter 执行了 {}", httpServletRequest.getRequestURI());
        System.err.println("MyFilter2 执行了 " + httpServletRequest.getRequestURI());
        filterChain.doFilter(servletRequest, servletResponse);
    }
}
